from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import oj_driver


# 测试搜索引擎登录界面

class Login:
    url = ""
    driver = ""

    # 定义构造函数
    def __init__(self):
        self.url = "http://111.230.81.226:8080/login.html"
        self.driver = oj_driver.driver
        self.driver.get(self.url)

    # 登录成功的测试用例
    def LoginSuccess(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("testonline")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("testonline")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 对弹窗的处理
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alertval = alert.text
        assert (alertval == "登录成功")
        # 点击确认
        alert.accept()
        oj_driver.getScreenShot()

    # 登录失败的测试用例
    def LoginFail(self):
        self.__usernamefail()
        self.__passwordfail()
        self.__allfail()

    # 各种登录失败的场景
    def __usernamefail(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("testonline123")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("testonline")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 对弹窗的处理
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alertval = alert.text
        assert (alertval == "Error: 登录失败")
        # 点击确认
        alert.accept()
        oj_driver.getScreenShot()

    def __passwordfail(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("testonline")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456789")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 对弹窗的处理
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alertval = alert.text
        assert (alertval == "Error: 登录失败")
        # 点击确认
        alert.accept()
        oj_driver.getScreenShot()

    def __allfail(self):
        # 清空输入框中的内容
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入测试用例
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("987654321")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("987654321")
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "#login-form > button:nth-child(6)").click()
        # 对弹窗的处理
        WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alertval = alert.text
        assert (alertval == "Error: 登录失败")
        # 点击确认
        alert.accept()
        oj_driver.getScreenShot()
